<?php

class RegistrationHandler
{
	private $m_db = NULL;
	
	public function __construct(Database $db)
	{
		$this->m_db = $db;
	}
	
	/* Checks so that the password is filled in according to the requirements */
	public function PasswordIsValid($password)
	{
		if (!is_string($password))
			return false;
		
		if (strlen($password) < 6)
			return false;
		
		return true;
	}
	
	public function PasswordMatches($password, $passconfirmation)
	{
		if ($password == NULL || $passconfirmation == NULL) return false;
		return ($password == $passconfirmation);
	}
	
	/* Checks so that the username is filled in according to the requirements */
	public function UsernameIsValid($username)
	{
		// If the chose usernae was too short
		if (strlen($username) == 0)
			return false;
		
		return true;
	}
	
	/* Checks if a user exists in the databse and returns TRUE/FALSE */
	public function UserExistsInDatabse($username)
	{
		$sqlQuery = "SELECT Username FROM User WHERE Username LIKE ?";
		$stmt = $this->m_db->Prepare($sqlQuery);
		$stmt->bind_param("s", $username);
		
		if ($this->m_db->ExecuteSelectOneQuery($stmt) != NULL)
			return true;
			 
		return false;
	}
	
	/* Adds a user to the databse */
	public function AddUser($username, $password)
	{
		if (!$this->UsernameIsValid($username) || !$this->PasswordIsValid($password))
			return false;
		
		if ($this->UserExistsInDatabse($username))
			return false;
		
		// Add to the databse
		$sqlQuery = "INSERT INTO User (username, password) VALUES (?, ?)";
		$stmt = $this->m_db->Prepare($sqlQuery);
		$stmt->bind_param("ss", $username, $password);
		
		$insertId = $this->m_db->ExecuteInsertQuery($stmt);
		
		return true;
	}
	
	/* Removes a user from the databse */
	public function RemoveUser($username)
	{
		if (!$this->UserExistsInDatabse($username))
			return false;
		
		// Remove user
		$sqlQuery = "DELETE FROM User WHERE username LIKE '$username'";
		$stmt = $this->m_db->Prepare($sqlQuery);
		
		$this->m_db->ExecuteRemoveOneQuery($stmt);
		
		return true;
	}
	
	/* Tests all the functionality of the RegistrationHandler */
	public static function Test()
	{
		// The system under test
		$sut = new RegistrationHandler();
		
		// Variable the indicates if any errors were found
		$hasErrors = false;
		
		// Test "PasswordIsValid()" with a too short password
		if ($sut->PasswordIsValid("four"))
		{
			echo "<div>Function PasswordIsValid() acts wrong when a too short password is entered.</div>";
			$hasErrors = true;
		}
		
		// Test "PasswordIsValid()" with a too long enough password
		if (!$sut->PasswordIsValid("morethansix"))
		{
			echo "<div>Function PasswordIsValid() acts wrong when a long enough password is entered.</div>";
			$hasErrors = true;
		}
		
		// Test Function "UsernameIsValid()" with an empty username
		if ($sut->UsernameIsValid(""))
		{
			echo "<div>Function UsernameIsValid() acts wrong when an empty username is entered.</div>";
			$hasErrors = true;
		}
		
		// Test Function "UsernameIsValid()" with a correct entered username
		if (!$sut->UsernameIsValid("testTesttest"))
		{
			echo "<div>Function UsernameIsValid() acts wrong when a correct username is entered.</div>";
			$hasErrors = true;
		}
		
		// Test Fucntion "AddUser()"
		if (!$sut->AddUser("testUser", "testPassword"))
		{
			echo "<div>Function AddUser() acts wrong when adding a new user.</div>";
			$hasErrors = true;
		}
		else
		{
			// Test function "UserExistsInDatabse()" with a user that exists
			if (!$sut->UserExistsInDatabse("testUser"))
			{
				echo "<div>Function UserExistsInDatabse() acts wrong when testing on a user that already exists.</div>";
				$hasErrors = true;
			}
			
			if (!$sut->RemoveUser("testUser"))
			{
				echo "<div>Function RemoveUser() acts wrong when removing a user that exists.</div>";
				$hasErrors = true;
			}
			else
			{
				// Test function "UserExistsInDatabse()" with a user that doesn't exists
				if ($sut->UserExistsInDatabse("testUser"))
				{
					echo "<div>Function UserExistsInDatabse() acts wrong when testing on a user that doesn't exists.</div>";
					$hasErrors = true;
				}				
			}
		}
		
		return !$hasErrors;
	}
}